// index.js
const mybehavior = require("../../../../../behaviors/requests")
// import {
//   widgetList
// } from "./test"
Page({
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/self_form_backend",
    fs_upload_url: "https://logisticsengineering.gree.com/self_form_backend/photo_manage",
    widgetList: [
    ],
    formField: {},
    selectShow: false,
    timePickerShow: false,
    selectOptions: [],
    pickerFieldId: "",
    selectPicker: {},
    checked: false,
    popup_date_show: false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    task_data: {},
    submit_btn: false,
    test:"123"

  },
  test() {
    // var responsbility = this.getWidgetRef('responsbility');
    // var responbility_man = this.getWidgetRef('responbility_man');
    // var responbility_value = responsbility.getValue();

    // switch (responbility_value) {
    //   case "工艺部": responbility_man.setValue("徐元直"); break;
    //   case "东区": responbility_man.setValue("杨大戈"); break;
    //   default: responbility_man.setValue("错误"); break;
    // }

  },
  onLoad() {
    this.getFormData()
  },
  // 获取表单JSON数据
  getFormData() {
    let url = this.data.baseUrl + "/new_form_manage/query_target_complete_data"
    let data = {
      key: "67341077f8efeca903f96b9d",
    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        console.log(res);
        this.setData({
          widgetList: res.data.vformJson.widgetList,
          task_data: res.data
        })
      } else {
        this.setData({
          widgetList: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        widgetList: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 检查必填项
  checkRequired() {
    let arr = this.data.widgetList
    let arr1 = this.data.formField
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].options.required == true && arr1[arr[i].options.name] == undefined) {
        wx.showToast({
          title: arr[i].options.label + '-不能为空！',
          icon: 'none'
        })
        return false
      }
    }
    return true

  },
  // 提交表单数据
  submit() {
    console.log(this.data);
    if (this.checkRequired() == true) {
      this.setData({
        submit_btn: true,
      })
      let url = this.data.baseUrl + "/form_application_manage/submit_request"
      let data = {
        application_id: "67341077f8efeca903f96b9d",
        form_data: this.data.formField
      }
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          console.log(res);
          wx.showToast({
            title: res.msg,
            icon: 'success'
          })
          this.reset()
          this.setData({
            submit_btn: false,
          })
        } else {
          this.setData({
            submit_btn: false,
          })
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      }).catch(err => {
        this.setData({
          submit_btn: false,
        })
        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }
  },
  // 重置按钮
  reset() {
    this.setData({
      formField: {},
    })
  },
  //输入框值变化
  inputChange(e) {
    // console.log(e);
    let key = e.target.dataset.item.options.name
    let obj = JSON.parse(JSON.stringify(this.data.formField))
    obj[key] = e.detail
    this.setData({
      formField: obj
    })
  },
  // 下拉选择框弹窗显示
  getPickerVlaue(e) {
    console.log(e);
    let key = e.target.id

    this.setData({
      [key]: e.detail.value
    })
  },
  // 上传照片
  getUploaderVlaue(e) {
    console.log(e);
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
  },
  // 点击输入框显示选择日期
  onDateClick(e) {
    let key = e.target.dataset.item.options.name
    this.setData({
      popup_date_show: true,
      pickerFieldId: key
    });
  },
  // 确定日期输入框
  onDateConfirm(e) {
    let obj = JSON.parse(JSON.stringify(this.data.formField))
    let val = this.formatDate1(new Date(e.detail))
    obj[this.data.pickerFieldId] = val
    this.setData({
      popup_date_show: false,
      formField: obj
    });
  },
  // 日期输入框关闭
  onDateClose() {
    this.setData({
      popup_date_show: false,
    });
  },
  getWidgetRef(e) {
    console.log(e);
  },
  onReady() {


  }
})